<template>
    <div style="font-size: 14px;height: calc(100% - 6.4rem)">

      <van-tree-select height="100%" :main-active-index.sync="active" :items="productTypes" @click-nav="selMenu">
        <template #content>
          <div class="typeName">
            {{type.text}}
          </div>

          <div style="background-color: #f5f5f5;height: 100%;">
            <van-grid :border="false" :column-num="1">
              <van-grid-item v-for="(item,index) in products" :key="index" style="margin: 0 0 10px 0;" @click="productDetail(item)">
                <div>
                  <van-image class="big"  :src="require('../../assets/img/'+item.img)" style="height: 200px;"/>
                </div>
                <div class="productName">
                  {{item.productName}}
                </div>
                <div class="price">
                  ¥{{item.price}}
                </div>
              </van-grid-item>
            </van-grid>
          </div>

          <van-empty v-if="products.length === 0" description="暂无商品" />

        </template>
      </van-tree-select>
    </div>
</template>

<script>
  import data from "@/data/data.json";
  import {
    Empty,
    Grid,
    GridItem,
    TreeSelect,
    Image as VanImage
  } from 'vant';

  export default {

    components: {
      [Empty.name]: Empty,
      [Grid.name]: Grid,
      [GridItem.name]: GridItem,
      [TreeSelect.name]: TreeSelect,
      [VanImage.name]: VanImage
    },

    data() {
      return {
        active: 0,
        type: {},//当前选中的分类
        productTypes:[],//商品分类
        products: [],//商品列表
      };
    },
    mounted() {
      //控制布局
      this.$store.commit('SET_HEADER_FOOTER', {showSearchHeader: true,headerName:'分类',pageName:'type',showFooter: true});
      this.productTypes = data.productTypes;
      this.selMenu(0);
    },
    methods: {
      selMenu(index){//切换菜单
        this.type = this.productTypes[index];
        this.products = [];
        data.products.forEach(item=>{
          item.typeId.forEach(id=>{
            if(id === this.type.id){
              this.products.push(item);
            }
          });
        });
      },
      productDetail(item){//商品详情
        this.$router.push({
          path:'/productDetail',
          query:{
            id: item.id
          }
        })
      }
    }
  };
</script>

<style scoped>
  .typeName{
    height: 40px;
    line-height: 40px;
    font-weight: 600;
    font-size: 18px;
    margin-left: 10px;
  }
  .productName {
    font-size: 16px;
    font-weight: 600;
    margin: 10px 10px 5px 10px;
    overflow: hidden;
    text-overflow: ellipsis;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    line-clamp: 2;
    -webkit-box-orient: vertical;
  }
  .price {
    color: red;
    font-weight: 600;
    font-size: 18px;
  }
</style>
